<template>
  <div class="content">
    <h1>管理员注册</h1>
    <div class="login_box">
      <el-form ref="form" :model="form" :rules="rules" label-width="60px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" type="age"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio v-model="form.sex" label="男">男</el-radio>
          <el-radio v-model="form.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="userLogin">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex';
export default {
  data() {
    return {
      account: [{ username: 'admin', password: '123456' }],
      form: {
        username: '',
        password: '',
        age: '',
        sex: '',
      },
      rules: {
        username: [
          { required: true, message: '请填写登录账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请填写登录密码', trigger: 'blur' },
        ],
        age: [{ required: true, message: '请填写年龄', trigger: 'blur' }],
        sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
      },
    };
  },
  methods: {
    userLogin() {
      this.$refs.form.validate().then(res => {
        if (res) {
          // 获取本地存储的账号列表
          var account = JSON.parse(localStorage.getItem('account'));
          // 新增注册的账号
          // console.log(account.unshift());
          account.unshift(this.form);
          localStorage.setItem('account', JSON.stringify(account));
          this.$message.success('注册成功');
          this.$router.push('/login');
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  height: 100vh;
  background-color: #545c64;
  overflow-y: hidden;
  h1 {
    text-align: center;
    color: #fff;
    margin-top: 30px;
  }
  .login_box {
    width: 400px;
    padding: 16px;
    margin: 100px auto;
    border-radius: 10px;
    background-color: #fff;
  }
}
</style>
